Headers and footers induced by the text

We have seen how we can use LATEX's marks to get information from the document contents to the headers and footers. The marks mechanism is the only reliable mechanism that you can use to get changing information to the headers or footers. This is because may be processing your document ahead before deciding to break the page.

Sometimes the two marks that offers are not enough. An example is the following:

If a solution to an exercise goes across a page break, then I would like to have ``(Continued on next page...)'' at the bottom of the first page and ``(Continued...)'' at the top in the margin of the next page.

You cannot use 's mark mechanisms for this if you also want to use chapter and section information.

The code from figure [*] constitutes a package that gives you two extra marks that can be used in this situation8.

Figure: Package for extra marks in
\begin{figure}\small
\index{extramarks@\texttt{\symbol{'134}extramarks}}
\begin{...
...@firstxmark ...
Here is a way to use this package:

\usepackage{extramarks}
...
\pagestyle{fancy}
\lhead{\firstxmark}
\rfoot{\lastxmark}
...
\extramarks{}{Continued on next page\ldots}
Some text that may or may not cross a page boundary...
\extramarks{Continued\ldots}{}

Note that the \extramarks command must be close to the text, i.e no empty lines (paragraph boundaries) should intervene. Otherwise the page may be broken at that boundary and the extramarks would come on the wrong page.

There are two new marks that can be used in the page layout with this package: If commands of the form \extramarks{m1}{m2} are given \firstxmark gives you the first m1 value and \lastxmark gives you the last m2 value of the current page. It also gives you the \firstleftmark and \lastrightmark commands that complement the standard marks.

To stress the point that marks are the correct way to do this, let me give you a ``solution'' that will not work9:

\lhead{Continued}
\rfoot{Continued on next page\ldots}
Some text that may or may not cross a page boundary...
\lhead{}
\rfoot{}

You may be tempted to think that the first \lhead and \rfoot will be in effect when / breaks the page in the middle of the text, and the last ones when the page breaks after the text. This is not true as the whole paragraph (including the last definitions) will be processed before / considers the page break, so at the time of the page break the last definitions are effective, whether the page break occurs inside the text or outside of it. Putting a paragraph boundary between the text and the last definitions will not work either, because you don't want the first definitions to be in effect when / decides to break the page exactly at this boundary. Actually the marks mechanism was invented to get rid of these kinds of problems.

In the above example the text ``Continued'' appears in the page header. It may be nicer to put it in the margin. This can be easily accomplished by positioning it at a fixed place relative to the page header. In plain / you would use a concoction of \hbox to 0pt, \vbox to 0pt, \hskip,\vskip, \hss and \vss but fortunately 's picture environment gives a much cleaner way to do this. In order not to disturb the normal header layout we put the text in a zero-sized picture. Generally this is the best way to position things on fixed places on the page. You can then also use the normal headings. See also section [*] for another example of this technique.

\setlength{\unitlength}{\baselineskip}
\lhead{\begin{picture}(0,0)
  \put(-2,-3){\makebox(0,0)[r]{\firstxmark}}
  \end{picture}\leftmark}

This solution can of course also be used for the footer. Be sure to put the picture as the first thing in left-handside entries and last in right-handside ones.

Finally you may want to put ``(Continued...)'' in the text rather than in the header or the margin. Then you have to use the afterpage package. We also decide to make a separate environment for it.

\newenvironment{continued}{\par
  \extramarks{}{Continued on next page\ldots}
  \afterpage{\noindent\firstxmark\vspace{1ex}}
  }{\extramarks{(Continued\ldots)}{}\par}

It is a bit dangerous to use \firstxmark outside the page layout routine, but apparently with \afterpage this works. If you would need the information further on in the page you must remember the state of the marks in your own variable. You can do this in one of the fancyheadings fields. For example if you want to add something after the broken piece of text you can use the following:

\newcommand{\mysaved}{}

\newenvironment{continued}{\par
  \extramarks{}{Continued on next page\ldots}
  }{\extramarks{(Continued\ldots)}{}\par\vspace{1ex}\mysaved}
\lhead{\leftmark}
\chead{\ifthenelse{\equal{\lastxmark}{}}
  {\gdef\mysaved{}}
  {\gdef\mysaved{\noindent[Continued from previous page]}}}

If you want to include one of the marks or other varying information in the saved text, you must use \xdef rather than \gdef.